
from e4ting.cache import RedisCache,INRC,StringID

class UploadCache(RedisCache):
    # 用于统计系统总共上传过多个文件
    file_id = INRC(attr="upload_num")
    def __init__(self, md5):
        super().__init__("upload", md5)

    def __del__(self):
        # 有效期为3650天
        self.timeout(315360000)

class TokenCache(RedisCache):
    # 用于记录系统总共处理的请求数量, 每执行一次 TokenCache.login_id 将自动 +1
    login_id = INRC(attr="login_num")

    def __init__(self, token, timeout=24*60*60):
        super().__init__("token", token)
        self.__expire__ = timeout

    def __del__(self):
        # 默认保留 1 day
        self.timeout(self.__expire__)

class DeviceCache(RedisCache):

    dev_id = INRC(attr="device_num")

    def __init__(self, uid):
        super().__init__("device", uid)

    def __del__(self):
        # 默认保留 30 day
        self.timeout(30*24*60*60)

class TaskCache(RedisCache):

    task_id = INRC(attr="task_num")

    def __init__(self, uid):
        super().__init__("task", uid)

    def __del__(self):
        # 默认保留 30 day
        self.timeout(30*24*60*60)

class OnlineCache(RedisCache):
    def __init__(self, uid):
        super().__init__("online", uid)

    def __del__(self):
        # 5min存活期
        self.timeout(300)

class UUIDCache(RedisCache):
    _id = INRC(attr="ws")

    def __init__(self, uuid, timeout=365*24*60*60):
        super().__init__("uuid", uuid)
        self.__expire__ = timeout

    @classmethod
    def cache(cls, func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            uuid = kwargs.get("uuid") or args[-1]
            temp = UUIDCache(uuid)
            if temp.exists():
                return temp.key
            ret = func(*args, **kwargs)
            if not ret:
                return ret

            temp.set(key=ret, uuid=uuid, uptime=util.now(), call=func.__name__, desc=func.__doc__)

            temp0 = UUIDCache(ret)
            temp0.set(key=ret, uuid=uuid, uptime=util.now(), desc=func.__doc__)

            return ret
        return wrapper

    def __del__(self):
        # 默认保留 1 day
        self.timeout(self.__expire__)

class FrpCache(RedisCache):

    frp_port = INRC(attr="frp_port")

    def __init__(self, uuid):
        super().__init__("frp", uuid)

    def __del__(self):
        # 保留 180 day
        self.timeout(15552000)

class HistoryCache(RedisCache):
    gpt_seq = StringID(attr="chatgpt")
    def __init__(self, uuid):
        super().__init__("chatgpt", uuid)

    def __del__(self):
        # 保留5天
        self.timeout(432000)

class WXChatCache(RedisCache):
    def __init__(self, uuid):
        super().__init__("wxmsg", uuid)

    def __del__(self):
        # 保留50天
        self.timeout(4320000)

class IPCache(RedisCache):
    def __init__(self, ip):
        super().__init__("location", ip)

    @classmethod
    def cache(cls, func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            uuid = kwargs.get("uuid") or args[-1]
            temp = cls(uuid)
            if temp.exists():
                return temp.key
            ret = func(*args, **kwargs)
            if not ret:
                return ret

            temp.set(key=ret, uuid=uuid, uptime=util.now(), call=func.__name__, desc=func.__doc__)

            temp0 = cls(ret)
            temp0.set(key=ret, uuid=uuid, uptime=util.now(), desc=func.__doc__)

            return ret
        return wrapper

    def __del__(self):
        # 保留5天
        self.timeout(432000)

class TemplateCache(RedisCache):
    key  = StringID(attr="template")
    def __init__(self, id):
        super().__init__("template", id)

    def __del__(self):
        # 保留1个月
        self.timeout(3600*24*365)
